package cz.muni.fi.pa165.bean;

import cz.muni.fi.pa165.dto.CustomerDTO;
import cz.muni.fi.pa165.dto.DeliveryDTO;
import cz.muni.fi.pa165.dto.EmployeeDTO;
import cz.muni.fi.pa165.dto.PackageDTO;
import cz.muni.fi.pa165.service.local.impl.CustomerService;
import cz.muni.fi.pa165.service.local.impl.DeliveryService;
import cz.muni.fi.pa165.service.local.impl.EmployeeService;
import cz.muni.fi.pa165.service.local.impl.PackageService;
import java.io.Serializable;
import java.text.DateFormat;
import java.text.ParseException;
import java.util.Date;
import java.util.List;
import javax.enterprise.context.SessionScoped;
import javax.enterprise.inject.Produces;
import javax.inject.Inject;
import javax.inject.Named;

/**
 *
 * @author <a href="mailto:vramik at redhat.com">Vlastislav Ramik</a>,Jakub Janco
 */
@SessionScoped
@Named
public class DeliveryManager implements Serializable{

    @Inject
    DeliveryService deliveryService;
    
    @Inject
    CustomerService customerService;
    
    @Inject
    EmployeeService employeeService;
    
    @Inject
    PackageService packageService;
    
    private String searchField;
    private String searchAttributeType;
    private String dateFrom;
    private String dateTo;
    private String searchCustomerId;
    private String searchDeliveryBoyId;
    private String addCustomerId;
    private String addDeliveryBoyId;
    private String addRecipientName;
    private String addRecipientAddress;
    private String addRecipientCity;
    private String addRecipientZip;
    private boolean creationSuccess;
    private Long createdDeliveryId;
    private DeliveryDTO delivery;
    private boolean deliverSuccess;

    //<editor-fold defaultstate="collapsed" desc="setters And Getters">
    public String getAddDeliveryBoyId() {
        return addDeliveryBoyId;
    }

    public void setAddDeliveryBoyId(String addDeliveryBoyId) {
        this.addDeliveryBoyId = addDeliveryBoyId;
    }
    
    public String getSearchField() {
        return searchField;
    }
    
    public void setSearchField(String searchField) {
        this.searchField = searchField;
    }
    
    public String getSearchAttributeType() {
        return searchAttributeType;
    }
    
    public void setSearchAttributeType(String searchAttributeType) {
        this.searchAttributeType = searchAttributeType;
    }
    
    public String getDateFrom() {
        return dateFrom;
    }
    
    public void setDateFrom(String dateFrom) {
        this.dateFrom = dateFrom;
    }
    
    public String getDateTo() {
        return dateTo;
    }
    
    public void setDateTo(String dateTo) {
        this.dateTo = dateTo;
    }
    
    public String getSearchCustomerId() {
        return searchCustomerId;
    }
    
    public void setSearchCustomerId(String searchCustomerId) {
        this.searchCustomerId = searchCustomerId;
    }
    
    public String getSearchDeliveryBoyId() {
        return searchDeliveryBoyId;
    }
    
    public void setSearchDeliveryBoyId(String searchDeliveryBoyId) {
        this.searchDeliveryBoyId = searchDeliveryBoyId;
    }
    
    public String getAddCustomerId() {
        return addCustomerId;
    }
    
    public void setAddCustomerId(String addCustomerId) {
        this.addCustomerId = addCustomerId;
    }
    
    public String getAddRecipientName() {
        return addRecipientName;
    }
    
    public void setAddRecipientName(String addRecipientName) {
        this.addRecipientName = addRecipientName;
    }
    
    public String getAddRecipientAddress() {
        return addRecipientAddress;
    }
    
    public void setAddRecipientAddress(String addRecipientAddress) {
        this.addRecipientAddress = addRecipientAddress;
    }
    
    public String getAddRecipientCity() {
        return addRecipientCity;
    }
    
    public void setAddRecipientCity(String addRecipientCity) {
        this.addRecipientCity = addRecipientCity;
    }
    
    public String getAddRecipientZip() {
        return addRecipientZip;
    }
    
    public void setAddRecipientZip(String addRecipientZip) {
        this.addRecipientZip = addRecipientZip;
    }

    public boolean isCreationSuccess() {
        return creationSuccess;
    }

    public void setCreationSuccess(boolean creationSuccess) {
        this.creationSuccess = creationSuccess;
    }

    public Long getCreatedDeliveryId() {
        return createdDeliveryId;
    }

    public void setCreatedDeliveryId(Long createdDeliveryId) {
        this.createdDeliveryId = createdDeliveryId;
    }

    public DeliveryDTO getDelivery() {
        return delivery;
    }

    public void setDelivery(DeliveryDTO delivery) {
        this.delivery = delivery;
    }
    
    //</editor-fold>

    public boolean isDeliverSuccess() {
        return deliverSuccess;
    }

    public void setDeliverSuccess(boolean deliverSuccess) {
        this.deliverSuccess = deliverSuccess;
    }

    
    @Produces
    @Named("deliveries")
    public List<DeliveryDTO> searchDeliveries() throws ParseException {
        DateFormat format;
        if (searchAttributeType == null) {
            return deliveryService.getAllDeliveries();
        }
        switch (searchAttributeType) {
            case "creationDate":
                if (isDateFromOrDateToNull()) {
                    return deliveryService.getAllDeliveries();
                }
                format = DateFormat.getTimeInstance();
                return deliveryService.getDeliveriesByCreationDate(format.parse(dateFrom), format.parse(dateTo));
            case "deliveryDate":
                if (isDateFromOrDateToNull()) {
                    return deliveryService.getAllDeliveries();
                }
                format = DateFormat.getTimeInstance();
                return deliveryService.getDeliveriesByDeliveryDate(format.parse(dateFrom), format.parse(dateTo));
            case "customer":
                if (searchCustomerId == null) {
                    return deliveryService.getAllDeliveries();
                }
                CustomerDTO customerDTO = customerService.getCustomerById(Long.parseLong(searchCustomerId));
                return deliveryService.getDeliveriesByCustomer(customerDTO);
            case "deliveryBoy":
                if (searchDeliveryBoyId == null) {
                    return deliveryService.getAllDeliveries();
                }
                EmployeeDTO employeeDTO = employeeService.getEmployeeById(Long.parseLong(searchDeliveryBoyId));
                return deliveryService.getDeliveriesByDeliveryBoy(employeeDTO);
            case "address":
                if (searchField == null) {
                    return deliveryService.getAllDeliveries();
                }
                return deliveryService.getDeliveriesByAddress(searchField);
            case "city":
                if (searchField == null) {
                    return deliveryService.getAllDeliveries();
                }
                return deliveryService.getDeliveriesByCity(searchField);
            case "recipient":
                if (searchField == null) {
                    return deliveryService.getAllDeliveries();
                }
                return deliveryService.getDeliveriesByRecipient(searchField);
            case "zip":
                if (searchField == null) {
                    return deliveryService.getAllDeliveries();
                }
                return deliveryService.getDeliveriesByZip(searchField);
            default:
                return deliveryService.getAllDeliveries();
        }
    }
    
    public void createDelivery() {
        creationSuccess = false;
        DeliveryDTO deliveryDTO = new DeliveryDTO();
        deliveryDTO.setCustomer(customerService.getCustomerById(Long.parseLong(addCustomerId)));
        deliveryDTO.setRecipientName(addRecipientName);
        deliveryDTO.setRecipientAddress(addRecipientAddress);
        deliveryDTO.setRecipientCity(addRecipientCity);
        deliveryDTO.setRecipientZip(addRecipientZip);
        deliveryDTO.setDeliveryBoy(employeeService.getEmployeeById(Long.parseLong(addDeliveryBoyId)));
        createdDeliveryId = deliveryService.createDelivery(deliveryDTO);
        addRecipientAddress = null;
        addRecipientCity = null;
        addRecipientName = null;
        addRecipientZip = null;
        creationSuccess = true;
    }

    private boolean isDateFromOrDateToNull() {
        return dateFrom == null || dateTo == null;
    }
    
    public void finishAddingPackages() {
        createdDeliveryId = null;
    }
    
    public void loadDelivery(Long id) {
        delivery = deliveryService.getDeliveryById(id);
    }
    
    public void deleteDelivery() {
        for (PackageDTO pkg : delivery.getPackages()) {
            packageService.removePackage(pkg);
        }
        deliveryService.removeDelivery(delivery);
    }
    
    public void deliver() {
        deliverSuccess=false;
        delivery.setDeliveryDate(new Date());
        deliveryService.updateDelivery(delivery);
        deliverSuccess=true;
    }
}
